package com.xxl.javaWork_15;

import java.io.*;

/**
 * @Author: xiaxinlin
 * @Date: 2020/12/22 21:29
 */
public class IOStreamDemo2 {

    public static void main(String[] args) {
        File file = new File("D:\\img\\test\\课件15：第四章 无穷级数 第二节 幂级数.wmv");
        File file1 = new File("D:\\img\\test\\file\\课件15：第四章 无穷级数 第二节 幂级数.wmv");
        try {
            IOStreamDemo2.copyFile(file,file1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            IOStreamDemo2.copyFileBf(file,file1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void copyFile(File source,File daddr) throws IOException {
        File file = daddr.getParentFile();
        if (!file.exists()){
            file.mkdirs();
        }
        long start = System.currentTimeMillis();
        FileInputStream inputStream = new FileInputStream(source);
        FileOutputStream outputStream = new FileOutputStream(daddr);
        int len;
        byte b[] = new byte[1024];

        while ((len=inputStream.read(b))!=-1){
            outputStream.write(b,0,len);
        }
        inputStream.close();
        outputStream.close();
        long end = System.currentTimeMillis();
        System.out.println("普通流测试时间："+(end-start)+"毫秒");
    }

    public static void copyFileBf(File source,File daddr) throws IOException {
        File file = daddr.getParentFile();
        if (!file.exists()){
            file.mkdirs();
        }
        long start = System.currentTimeMillis();
        BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(source));
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(daddr));
        int len;
        byte b[] = new byte[1024];

        while ((len=inputStream.read(b))!=-1){
            outputStream.write(b,0,len);
        }
        inputStream.close();
        outputStream.close();
        long end = System.currentTimeMillis();
        System.out.println("缓冲流测试时间："+(end-start)+"毫秒");
    }
}
